package com.curvebreakers.wheresmystuff.activities;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.model.*;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class CreateEntryFragment extends Fragment implements DBResultListener {
	private ProgressDialog progressDialog;
	private TabSwitcher switcher;
	
	private EditText titleField, descriptionField, rewardField, zipCodeField;
	private CheckBox rewardCheckBox;
	private Spinner categoryField;
	private View rewardContainer;

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_create_entry, container,false);
		titleField = (EditText)view.findViewById(R.id.title);
		descriptionField = (EditText)view.findViewById(R.id.description);
		categoryField = (Spinner)view.findViewById(R.id.categorySpinner);
		rewardCheckBox = (CheckBox)view.findViewById(R.id.rewardCheckBox);
		rewardField = (EditText)view.findViewById(R.id.rewardAmount);
		rewardContainer = view.findViewById(R.id.rewardContainer);
		zipCodeField = (EditText)view.findViewById(R.id.zip_code);
	
		ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(getActivity(), android.R.layout.simple_spinner_item, Category.getPostCategories());
		categoryField.setAdapter(adapter);
		
		rewardCheckBox.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
					rewardField.setEnabled(rewardCheckBox.isChecked());
			}
		});
		
		
		switcher = new TabSwitcher(view.findViewById(R.id.lostTab));
		
		view.findViewById(R.id.lostTab).setOnClickListener(switcher);
		view.findViewById(R.id.foundTab).setOnClickListener(switcher);
		view.findViewById(R.id.donationTab).setOnClickListener(switcher);
		
		view.findViewById(R.id.createButton).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Entry entry = createEntry();
				if (entry != null) {
					progressDialog = ProgressDialog.show(getActivity(), "Creating New Entry...", "Please Wait");
					DatabaseModel.saveEntry(entry, CreateEntryFragment.this);
				}		
			}
		});
		
		
		return view;
	}
	
	/**
	 * Creates an entry from the elements in the form and the current users information
	 * 
	 * @return the create entry
	 */
	public Entry createEntry() {
		int userId = Session.getSession().getUser().getId();
		String email = Session.getSession().getUser().getEmail();
		String title = titleField.getText().toString();
		String description = descriptionField.getText().toString();
		Category category = (Category)categoryField.getSelectedItem();
		String zipCode = zipCodeField.getText().toString();
		EntryType type = switcher.getEntryType();
		int reward = 0;
		if (rewardCheckBox.isChecked() && type == EntryType.LOST)
				reward = Integer.parseInt(rewardField.getText().toString());

		//Do form validation
		boolean valid = true;
		
		if (title.length() == 0) {
			titleField.setError("Please enter a title.");
			valid = false;
		} else {
			titleField.setError(null);
		}
		
		Entry entry = null;
		if (valid)
			entry = new Entry.Builder().title(title).description(description).category(category).entryType(switcher.getEntryType()).reward(reward)
				.userId(userId).resolved(false).userEmail(email).zipCode(zipCode).build();
//			entry = new Entry(userId, title, description, category, switcher.getEntryType(), reward, false, null, email, null);
		return entry;

	}
	
	/**
	 * A listener class for the Entry type tab switcher
	 */
	private class TabSwitcher implements View.OnClickListener {
		
		View selected;
		
		public TabSwitcher(View selected) {
			this.selected = selected;
			selected.setBackgroundResource(R.drawable.ab_transparent_wmstheme);
		}

		@Override
		public void onClick(View v) {
			selected.setBackgroundColor(Color.TRANSPARENT);
			selected = v;
			v.setBackgroundResource(R.drawable.ab_transparent_wmstheme);
			
			if (v.getId() == R.id.lostTab) {
				rewardContainer.setVisibility(View.VISIBLE);
			} else {
				rewardContainer.setVisibility(View.GONE);
			}
				
		}
		
		public EntryType getEntryType() {
			switch (selected.getId()) {
				case R.id.lostTab:
					return EntryType.LOST;
				case R.id.foundTab:
					return EntryType.FOUND;
				case R.id.donationTab:
					return EntryType.DONATION;
				default:
					return null;
			}
		}
	}

	
	@Override
	public void onDBResultReceived(Object obj) {
		progressDialog.dismiss();
		Toast.makeText(getActivity(), "New entry succesfully created...", Toast.LENGTH_LONG).show();
		//Clear the form
		titleField.setText("");
		descriptionField.setText("");
		rewardCheckBox.setChecked(false);
		rewardField.setText("");
		rewardField.setEnabled(false);
		rewardField.setText("");
		zipCodeField.setText("");
		categoryField.setSelection(0);
		((HomeActivity)getActivity()).switchToTab(0);
	}

	@Override
	public void onDBRequestFailure(String message) {
		progressDialog.dismiss();
		Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
	}	
}

